package com.ruoyi.project.propertySystem.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.propertySystem.domain.InspectItem;
import com.ruoyi.project.propertySystem.mapper.InspectItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.propertySystem.mapper.InspectClassifyMapper;
import com.ruoyi.project.propertySystem.domain.InspectClassify;
import com.ruoyi.project.propertySystem.service.IInspectClassifyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 巡检分类Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-18
 */
@Service
public class InspectClassifyServiceImpl implements IInspectClassifyService 
{
    @Autowired
    private InspectClassifyMapper inspectClassifyMapper;
    @Autowired
    private InspectItemMapper inspectItemMapper;

    /**
     * 查询巡检分类
     * 
     * @param id 巡检分类ID
     * @return 巡检分类
     */
    @Override
    public InspectClassify selectInspectClassifyById(Long id)
    {
        return inspectClassifyMapper.selectInspectClassifyById(id);
    }

    /**
     * 查询巡检分类列表
     * 
     * @param inspectClassify 巡检分类
     * @return 巡检分类
     */
    @Override
    public List<InspectClassify> selectInspectClassifyList(InspectClassify inspectClassify)
    {
        return inspectClassifyMapper.selectInspectClassifyList(inspectClassify);
    }

    /**
     * 新增巡检分类
     * 
     * @param inspectClassify 巡检分类
     * @return 结果
     */
    @Override
    public int insertInspectClassify(InspectClassify inspectClassify)
    {
        inspectClassify.setCreateBy(SecurityUtils.getUsername());
        inspectClassify.setGmtCreate(new Date());
        int i = inspectClassifyMapper.insertInspectClassify(inspectClassify);
        String str = "C" + String.format("%06d", inspectClassify.getId());
        inspectClassify.setClassifySn(str);
        inspectClassifyMapper.updateInspectClassify(inspectClassify);
        return i;
    }

    /**
     * 修改巡检分类
     * 
     * @param inspectClassify 巡检分类
     * @return 结果
     */
    @Override
    public int updateInspectClassify(InspectClassify inspectClassify)
    {
        return inspectClassifyMapper.updateInspectClassify(inspectClassify);
    }

    /**
     * 批量删除巡检分类
     * 
     * @param ids 需要删除的巡检分类ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteInspectClassifyByIds(Long[] ids)
    {
        Arrays.stream(ids).forEach(x->{
            List<InspectItem> inspectItems = inspectItemMapper.selectInspectItemByClassifyId(x);
            if(inspectItems.size()>0){
                throw new BaseException("该分类下存在巡检项，不能删除");
            }
        });
        return inspectClassifyMapper.deleteInspectClassifyByIds(ids);
    }

    /**
     * 删除巡检分类信息
     * 
     * @param id 巡检分类ID
     * @return 结果
     */
    @Override
    public int deleteInspectClassifyById(Long id)
    {
        return inspectClassifyMapper.deleteInspectClassifyById(id);
    }
}
